package com.test.qpbgboot.backend.service.impl;

import com.test.qpbgboot.backend.mapper.MerchantAccountLogMapper;
import com.test.qpbgboot.backend.mapper.MerchantMapper;
import com.test.qpbgboot.backend.pojo.dto.MerchantAccountLogQuery;
import com.test.qpbgboot.backend.pojo.entity.Merchant;
import com.test.qpbgboot.backend.pojo.entity.MerchantAccountLog;
import com.test.qpbgboot.backend.pojo.vo.MerchantAccountLogVO;
import com.test.qpbgboot.backend.service.MerchantAccountService;
import com.test.qpbgboot.base.enums.GeneralStatusEnum;
import com.test.qpbgboot.base.enums.TransactionTypeEnum;
import com.test.qpbgboot.base.exception.ServiceException;
import com.test.qpbgboot.base.response.ResultStatus;
import com.test.qpbgboot.show.mapper.ShopMapper;
import com.test.qpbgboot.show.pojo.dto.ShopQuery;
import com.test.qpbgboot.show.pojo.vo.ShopVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class MerchantAccountServiceImpl implements MerchantAccountService {

    @Autowired
    private MerchantAccountLogMapper merchantAccountLogMapper;
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private ShopMapper shopMapper;

    @Override
    public List<MerchantAccountLogVO> merchantAccountLogSelect(
            MerchantAccountLogQuery merchantAccountLogQuery) {
        log.debug("商家账户记录查询业务，参数:{}", merchantAccountLogQuery);
        List<MerchantAccountLogVO> accountLogVOList = merchantAccountLogMapper
                .selectVObyQuery(merchantAccountLogQuery);
        log.debug("商家账户记录查询结果:{}", accountLogVOList);
        return Collections.emptyList();
    }

    @Override
    public void merchantTransactionRecord(MerchantAccountLog accountLog) {
        log.debug("商家账户交易记录业务，参数:{}", accountLog);
        if (accountLog == null) {
            throw new ServiceException(ResultStatus.FAILURE, "无数据");
        }

        if (merchantMapper.selectEnableById(accountLog.getMerchantId()) != 1) {
            throw new ServiceException(ResultStatus.DIRTY_DATA, "无效的商家id");
        }
        ShopQuery shopQuery = new ShopQuery();
        shopQuery.setId(accountLog.getShopId());
        List<ShopVO> shopVOList = shopMapper.selectShopVObyShopsQuery(shopQuery);
        if (shopVOList == null || !shopVOList.get(0).getStatus()
                .equals(GeneralStatusEnum.ENABLE.getValue())) {
            throw new ServiceException(ResultStatus.DIRTY_DATA, "无效的店家id");
        } else if (!Objects.equals(shopVOList.get(0).getMerchantId(),
                accountLog.getMerchantId())) {
            throw new ServiceException(ResultStatus.ILLEGAL_ARGUMENT, "店家id和商家id不匹配");
        }

        if (accountLog.getId() == null) {
            merchantAccountLogMapper.insert(accountLog);
        } else {
            MerchantAccountLogVO logVO = merchantAccountLogMapper
                    .selectVObyId(accountLog.getId());
            if (logVO == null || !Objects.equals(logVO.getOrderNo(),
                    accountLog.getOrderNo())) {
                throw new ServiceException(ResultStatus.DIRTY_DATA, "商家账户记录有误");
            }
            merchantAccountLogMapper.update(accountLog);
        }

        List<String> positiveTypeList = new ArrayList<>();
        positiveTypeList.add(TransactionTypeEnum.TRANSFER_INCOME.getValue());
        List<String> negativeTypeList = new ArrayList<>();
        negativeTypeList.add(TransactionTypeEnum.TRANSFER_EXPENSES.getValue());
        negativeTypeList.add(TransactionTypeEnum.REFUND.getValue());
        negativeTypeList.add(TransactionTypeEnum.DIRECT_REFUND.getValue());

        String type = accountLog.getTransactionType();
        BigDecimal accountBalance = merchantMapper
                .selectAccountBalanceById(accountLog.getMerchantId());
        if (positiveTypeList.contains(type)) {
            accountBalance = accountBalance.add(accountLog.getAmount());
        } else if (negativeTypeList.contains(type)) {
            accountBalance = accountBalance.subtract(accountLog.getAmount());
        }

        Merchant merchant = new Merchant();
        merchant.setId(accountLog.getMerchantId());
        merchant.setAccountBalance(accountBalance);
        merchantMapper.changeBalance(merchant);

    }
}
